#include "pwm.h"
#include "led.h"
#include "delay.h"
#include "speed_reading.h"

//PWM输出初始化
//arr：自动重装值
//psc：时钟预分频数
extern u16 System_Time_Counter;
unsigned char PWM1Control=1;
unsigned char TIM1Flag=0;
unsigned char PWM1Reverse=0;
float e[4],e1[4],e2[4];
float duk[4],uk[4],uk1[4],out[4];
float Kp[4]={0.3,0.3,0.3,0.3};
float Ki[4]={0.1,0.1,0.1,0.1};
float Kd[4]={0.01,0.01,0.01,0.01};
u16 PWM1_Max=898;
u16 PWM1_Min=10;
u16 Actual_Output[4],PWM1_value[4];


void PWM_init(u16 arr,u16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);// 
 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);  //使能GPIO外设时钟使能
	                                                                     	

   //设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

	
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 80K
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值  不分频
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM8, &TIM_OCInitStructure);  //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
	TIM_OC2Init(TIM8, &TIM_OCInitStructure);  //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
	TIM_OC3Init(TIM8, &TIM_OCInitStructure);
	TIM_OC4Init(TIM8, &TIM_OCInitStructure);

  	TIM_CtrlPWMOutputs(TIM8,ENABLE);	//MOE 主输出使能	

	TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH1预装载使能	 
	
	TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIMx在ARR上的预装载寄存器
	for(int i=0;i<4;i++)
		PWM1_value[i]=PWM1_Min;
	TIM_Cmd(TIM8, ENABLE);  //使能TIM1
}

void PID_Speed_Calc(int Motor)
{
	e[Motor]=Target_Inpluse[Motor]-Current_Inpluse[Motor];//计算当前偏差值变量

	duk[Motor]=(Kp[Motor]*(e[Motor]-e1[Motor])+Ki[Motor]*e[Motor]+Kd[Motor]*(e[Motor]-2*e1[Motor]+e2[Motor]));//PID连续系统离散化增量型PID算法，算出总偏差值变量。
	uk[Motor]=duk[Motor]+uk1[Motor];//计算偏差值总变量加上之后偏差值总变量之和赋给目前总偏差值变量
	out[Motor]=uk[Motor];//目前总偏差值变量赋给PID调节后输出偏差值变量
	Actual_Output[Motor]=out[Motor];
	if(Actual_Output[Motor]>PWM1_Max)//判断PID调节后输出偏差值变量是否大于额定PWM限制
	{
		Actual_Output[Motor]=PWM1_Max;//PID调节后输出偏差值变量为限制内
	}
	else if(Actual_Output[Motor]<PWM1_Min)//判断PID调节后输出偏差值变量是否小于0
	{
		Actual_Output[Motor]=PWM1_Min;//PID调节后输出偏差值变量为0
	}
	uk1[Motor]=uk[Motor];//目前总偏差值变量赋给之后偏差值总变量
	e2[Motor]=e1[Motor];//之前偏差值变量赋给之后偏差值变量
	e1[Motor]=e[Motor];//当前偏差值变量赋给之前偏差值变量
	PWM1_value[Motor]=Actual_Output[Motor];//PID调节后输出偏差值变量赋给脉冲宽度时间变量
	
}

